package com.woniu.cloud.storeassembly.apiimpl;

import com.woniu.cloud.api.StoreLoginApi;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.entity.TStoreLogin;
import com.woniu.cloud.form.StoreLoginForm;
import com.woniu.cloud.form.UpdateStoreLoginForm;
import com.woniu.cloud.storeassembly.service.ITStoreLoginService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
public class StoreLoginApiImpl implements StoreLoginApi {

    @Resource
    private ITStoreLoginService storeLoginService;

    @Override
    @GetMapping("getStoreLoginBynameAndWord")
    @GlobalTransactional
    public Result getStoreLoginBynameAndWord(@RequestParam("username") String username,
                                             @RequestParam("password") String password) {
        log.info("通过用户信息查找是否存在->username:{},password:{}",username,password);
        if (null==username||null==password) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始查询->username:{},password:{}",username,password);
            return storeLoginService.getStoreLoginByLoginId(username,password);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }

    @Override
    @PostMapping("insertStoreLogin")
    @GlobalTransactional
    public Result insertStoreLogin(@RequestBody StoreLoginForm storeLoginForm) {
        log.info("添加账号->storeLoginForm:{}",storeLoginForm);
        if (null==storeLoginForm) {
            return Result.fail("参数异常",null);
        }
        try {
            TStoreLogin storeLogin = new TStoreLogin();
            BeanUtils.copyProperties(storeLoginForm,storeLogin);
            log.info("开始添加->storeLogin:{}",storeLogin);
            return storeLoginService.insertStoreLogin(storeLogin);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }


    @Override
    @PutMapping("updateStoreLoginByStoreLoginId")
    @GlobalTransactional
    public Result updateStoreLoginByStoreLoginId(@RequestBody UpdateStoreLoginForm updateStoreLogin) {
        log.info("修改账号->updateStoreLogin:{}",updateStoreLogin);
        if (null==updateStoreLogin.getStoreLoginId()) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始修改->updateStoreLogin:{}",updateStoreLogin);
            TStoreLogin storeLogin = new TStoreLogin();
            BeanUtils.copyProperties(updateStoreLogin,storeLogin);
            return storeLoginService.updateStoreLoginByStoreLoginId(storeLogin);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }

    @Override
    @DeleteMapping("deleteStoreLoginByStoreLoginId")
    @GlobalTransactional
    public Result deleteStoreLoginByStoreLoginId(@RequestParam("storeLoginId") Integer storeLoginId) {
        log.info("根据id删除账号->storeLoginId:{}",storeLoginId);
        try {
            log.info("开始删除->storeLoginId:{}",storeLoginId);
            return storeLoginService.deleteStoreLoginByStoreLoginId(storeLoginId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",false);
        }
    }
}
